package com.xuecheng.manage_cms.service;

import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.request.QueryPageRequest;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.manage_cms.dao.CmsPageRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class CmsPageService {
    @Autowired
    private CmsPageRepository cmsPageRepository;

    //分页查询
    public QueryResponseResult findList(Integer pageNum, Integer pageSize, QueryPageRequest request) {
        if (ObjectUtils.isEmpty(request)) {
            request = new QueryPageRequest();
        }
        CmsPage cmsPage = new CmsPage();
        //判断并添加查询条件
        //id
        if (StringUtils.isNotEmpty(request.getPageId())) {
            cmsPage.setPageId(request.getPageId());
        }
        //站点id
        if (StringUtils.isNotEmpty(request.getSiteId())) {
            cmsPage.setSiteId(request.getSiteId());
        }
        //别名
        if (StringUtils.isNotEmpty(request.getPageAliase())) {
            cmsPage.setPageAliase(request.getPageAliase());
        }
        //模糊查询对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<CmsPage> example = Example.of(cmsPage, exampleMatcher);

        //分页之前最好先进行分页参数处理
        if (pageNum <= 0) {
            //为了前端交互方便，使用1为起始页
            pageNum = 1;
        }
        if (pageSize <= 0) {
            pageSize = 10;
        }
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        //分页查询
        Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);
        //封装返回参数
        QueryResult<CmsPage> queryResult = new QueryResult<>();
        queryResult.setTotal(all.getTotalElements());
        queryResult.setList(all.getContent());
        //返回对象
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }


    /**
     * 添加页面对象
     *
     * @param cmsPage
     * @return
     */
    public CmsPageResult add(CmsPage cmsPage) {
        //首先需要根据站点id、页面名称和页面webpath查询是否已经存在，该三个参数组成了唯一索引
        if (!ObjectUtils.isEmpty(cmsPage)) {
            CmsPage resultCmsPage = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());
            if (ObjectUtils.isEmpty(resultCmsPage)) {
                //为空说明不重复，可以添加
                cmsPageRepository.save(cmsPage);
                return new CmsPageResult(CommonCode.SUCCESS, cmsPage);
            }

        }
        //添加失败
        return new CmsPageResult(CommonCode.FAIL, null);
    }
}
